package com.snowcattle.game.service.rpc.client;

import com.snowcattle.game.bootstrap.manager.LocalManager;
import com.snowcattle.game.common.config.GameServerDiffConfig;
import com.snowcattle.game.common.constant.Loggers;
import com.snowcattle.game.common.constant.ServiceName;
import com.snowcattle.game.common.enums.BOEnum;
import com.snowcattle.game.service.IService;
import com.snowcattle.game.service.config.GameServerConfigService;
import com.snowcattle.game.service.rpc.client.impl.DbRpcConnectManager;
import com.snowcattle.game.service.rpc.client.impl.GameRpcConnectedManager;
import com.snowcattle.game.service.rpc.client.impl.WorldRpcConnectManager;
import com.snowcattle.game.service.rpc.server.SdServer;
import com.snowcattle.game.service.rpc.server.zookeeper.ZooKeeperNodeBoEnum;
import com.snowcattle.game.service.rpc.server.zookeeper.ZooKeeperNodeInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * rpc的服务发现
 */
@Service
public class RpcClientConnectService implements IService {

    private static final Logger LOGGER = Loggers.rpcLogger;

    protected Object lock = new Object();

    @Autowired
    private WorldRpcConnectManager worldRpcConnectManager;

    @Autowired
    private GameRpcConnectedManager gameRpcConnectedManager;

    @Autowired
    private DbRpcConnectManager dbRpcConnectManager;

    public void initWorldConnectedServer(List<SdServer> sdServerList) throws Exception {
        GameServerConfigService gameServerConfigService = LocalManager.getInstance().getLocalSpringServiceManager().getGameServerConfigService();
        worldRpcConnectManager.initServers(sdServerList);
    }

    public void initGameConnectedServer(List<SdServer> sdServerList) throws Exception {
        GameServerConfigService gameServerConfigService = LocalManager.getInstance().getLocalSpringServiceManager().getGameServerConfigService();
        gameRpcConnectedManager.initServers(sdServerList);
    }

    public void initDbConnectServer(List<SdServer> sdServerList) throws Exception {
        GameServerConfigService gameServerConfigService = LocalManager.getInstance().getLocalSpringServiceManager().getGameServerConfigService();
        dbRpcConnectManager.initServers(sdServerList);
    }

    @Override
    public String getId() {
        return ServiceName.RpcServiceDiscovery;
    }

    @Override
    public void startup() throws Exception {
        worldRpcConnectManager.initManager();
        gameRpcConnectedManager.initManager();
        dbRpcConnectManager.initManager();
        GameServerConfigService gameServerConfigService = LocalManager.getInstance().getLocalSpringServiceManager().getGameServerConfigService();
        GameServerDiffConfig gameServerDiffConfig = gameServerConfigService.getGameServerDiffConfig();
        if (!gameServerDiffConfig.isZookeeperFlag()) {
            init();
        }
    }

    @Override
    public void shutdown() throws Exception {
        worldRpcConnectManager.stop();
        gameRpcConnectedManager.stop();
        dbRpcConnectManager.stop();
    }

    @SuppressWarnings("unchecked")
    public void init() throws Exception {
        GameServerConfigService gameServerConfigService = LocalManager.getInstance().getLocalSpringServiceManager().getGameServerConfigService();
        initWorldConnectedServer(gameServerConfigService.getRpcServerRegisterConfig().getSdWorldServers());
        initGameConnectedServer(gameServerConfigService.getRpcServerRegisterConfig().getSdGameServers());
        initDbConnectServer(gameServerConfigService.getRpcServerRegisterConfig().getSdDbServers());
    }


    public AbstractRpcConnectManager getRpcConnectMannger(BOEnum boEnum) {
        AbstractRpcConnectManager abstractRpcConnectManager = worldRpcConnectManager;
        if (boEnum.equals(BOEnum.GAME)) {
            abstractRpcConnectManager = gameRpcConnectedManager;
        } else if (boEnum.equals(BOEnum.DB)) {
            abstractRpcConnectManager = dbRpcConnectManager;
        }
        return worldRpcConnectManager;
    }

    public AbstractRpcConnectManager getRpcConnectMannger(ZooKeeperNodeBoEnum zooKeeperNodeBoEnu) {
        AbstractRpcConnectManager abstractRpcConnectManager = worldRpcConnectManager;
        if (zooKeeperNodeBoEnu.equals(ZooKeeperNodeBoEnum.GAME)) {
            abstractRpcConnectManager = gameRpcConnectedManager;
        } else if (zooKeeperNodeBoEnu.equals(ZooKeeperNodeBoEnum.DB)) {
            abstractRpcConnectManager = dbRpcConnectManager;
        }
        return worldRpcConnectManager;
    }

    public void notifyConnect(ZooKeeperNodeBoEnum zooKeeperNodeBoEnum, List<ZooKeeperNodeInfo> zooKeeperNodeInfoList) throws InterruptedException {
        getRpcConnectMannger(zooKeeperNodeBoEnum).initZookeeperRpcServers(zooKeeperNodeInfoList);
    }

}

